package com.education.springbootdemo.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.education.springbootdemo.domain.Resp;
import com.education.springbootdemo.exception.UserBizException;
import com.education.springbootdemo.service.UserService;
import com.education.springbootdemo.utils.TokenUtils;
import com.education.springbootdemo.vo.Token;
import com.education.springbootdemo.vo.User;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

@RestController
@RequestMapping("")
@Validated
public class LoginController {

    @Autowired
    private UserService userService;

    //httpstatus 404， /login
    //{ id:1 }
    @RequestMapping(path = "/login",method = RequestMethod.POST)
    public Resp<Token> login(@RequestBody  User puser) {
        User user = userService.selectByUsernameAndPassword(puser.getUsername(),puser.getPassword());
        if(user==null) {
            return Resp.error(1002,"找不到用户");
        }

       Token token = TokenUtils.CreateToken(user.getId(),null);

        return Resp.success(token);
    }


    @RequestMapping(path = "/login_info",method = RequestMethod.POST)
    public Resp<Boolean> getLoginInfo() {
        return Resp.success(null);
    }

    @RequestMapping("/reg")
    public boolean reg(@RequestBody User user) {
        user = userService.selectByUsername(user.getUsername());
        if(user==null) {
            user = userService.reg(user.getUsername(),user.getPassword());
            return user!=null;
        }
        return false;
    }

    @GetMapping("/insert")
    public boolean insert(@RequestBody User user) {
        userService.insertUser(user);
        return false;
    }

    @GetMapping("/ex")
    public boolean gg() throws UserBizException {
        try{
            int i = 1/0;
            return i==0;
        }catch(Exception e) {
            throw new UserBizException(e);
        }
    }
}
